import axios from 'axios';

let register = (function () {
    /*
    * 负责登陆功能的实现
    * */

    let registerComponent = {
        template:
        '<form id="registerForm" enctype="multipart/form-data">' +
            '<input class="registerInput" type="text" />'+
            '<input class="registerInput" type="password" />'+
            '<button type="button" @click="emit()">登陆</button>'+
        '</form>',
        methods:{
            emit:function () {
                console.log('emit');
                let url='register';
                let registerArr = document.getElementsByClassName('registerInput');
                let values = this.getInputContent(registerArr);
                let data = {
                    name:values[0],
                    password:values[1]
                };
                axios.post(url,data).then(function (res) {
                    console.log(res);
                    window.location.href=res.data;
                });
            },
            getInputContent:function (inputArr) {
                let valueArr = [];
                for(let i=0;i<inputArr.length;i++){
                    valueArr.push(inputArr[i].value);
                }
                return valueArr;
            }
        }
    };

    let root = {
        el:'#register',
        template:'<register-component></register-component>',
        data:{

        },
        components:{
            'register-component':registerComponent
        }
    };

    return {
        root:root
    }
})();

export default register;